var util = {
	useLocalStorage:false,
	setCookie : function(e, t, o) {
		var n = new Date;
		o || (o = 30), n.setTime(n.getTime() + 24 * o * 60 * 60 * 1e3),
				document.cookie = e + "=" + escape(t) + ";expires="
						+ n.toGMTString() + ";path=/"
	},
	getCookie : function(e) {
		var t, o = new RegExp("(^| )" + e + "=([^;]*)(;|$)");
		if (t = document.cookie.match(o)) {
			var n = unescape(t[2]);
			return n ? unescape(t[2]).replace(/^\"|\"$/g, "")
					: unescape(t[2])
		}
		return null
	},
	setLStorage:function(e, t){
		window.localStorage.setItem(e, t);
	},
	getLStorage:function(e){
		return window.localStorage.getItem(e);
	},
	removeLStorage : function(e) {
		window.localStorage.removeItem(e)
	},
	setSStorage:function(e, t){
		window.sessionStorage.setItem(e, t);
	},
	getSStorage:function(e){
		return window.sessionStorage.getItem(e);
	},
	removeSStorage : function(e) {
		window.sessionStorage.removeItem(e)
	},
	setLocalStorage : function(e, t) {
		if(this.useLocalStorage){
			window.localStorage.setItem(e, t)
		}else{
			window.sessionStorage.setItem(e, t)
		}
	},
	getLocalStorage : function(e) {
		if(this.useLocalStorage){
			return window.localStorage.getItem(e)
		}else{
			return window.sessionStorage.getItem(e)
		}
	},
	
	//----JSON Object
	setJsonLocals : function (key, val) {
	    this.setLocalStorage(key, JSON.stringify(val));
	},
	getJsonLocals : function (e) {
	    var o = this.getLocalStorage(e) || "{}"
	      , n = JSON.parse(o);
	    return n;
	},
	setJsonLocalsAttr : function(key,attr,val){
		  var o = this.getLocalStorage(key) || "{}"
	      , n = JSON.parse(o);
		 n[attr] = val;
		 this.setLocalStorage(key, JSON.stringify(n));
	},
	getJsonLocalsAttr : function(key,attr){
		  var o = this.getLocalStorage(key) || "{}"
	      , n = JSON.parse(o);
		 return  n[attr];
	},
	removeLocalStorage : function(e) {
		if(this.useLocalStorage){
			window.localStorage.removeItem(e)
		}else{
			window.sessionStorage.removeItem(e)
		}
		
	},
	removeAllLocalStorage : function() {
		if(this.useLocalStorage){
			window.localStorage.clear()
		}else{
			window.sessionStorage.clear()
		}
		
	},
	setJsonArrayLocal : function(key, val) {
		var o = JSON.parse(this.getLocalStorage(key) || "[]");
		o.push(val);
		this.setLocalStorage(key, JSON.stringify(o));
	},
	setJsonArrayLocalOfIndex:function(key, val ,index){
		var o = JSON.parse(this.getLocalStorage(key) || "[]");
		o[index] = val;
		this.setLocalStorage(key, JSON.stringify(o));
	},
	getJsonArrayLocal : function(key, val) {
		var o = JSON.parse(this.getLocalStorage(key) || "[]");
		var i;
		$.each(o, function(index, item){
		    if(val.id == item.id){
		    	i = item;
		    	return false;
		    }
		}); 
		return i;
	},
	removeJsonArrayLocal : function(key, i) {
		var o = JSON.parse(this.getLocalStorage(key) || "[]");
		o.splice(i,1);
		this.setLocalStorage(key, JSON.stringify(o));
	},
	getJsonAllArrayLocal : function(key) {
		return JSON.parse(this.getLocalStorage(key) || "[]");
	},
	getBaiduAk:function(){
		 var o = ["uNQ0osP6knV1wSGrf8H9pLcc", "iRjKG68G0pIEOgT1CmcSylUx", "KKGingsUc3oIA2xr1keHjQMb",
	                "0F83GQQjTqD79ZKaqsGrztAv", "RsSOGym0UDOYqh8hTAXErio9", "UG1rnFLKpMyjEnsBYkcazk6X",
	                "iLb13C14vXWQ1fq5Y0RfRoGY", "Z3jnjfj1rG0ECD68jWPulM1v", "7K7aRmtQNdHAnjCdW18a2CRp",
	                "gYGn5PHbroP7nOrq0TF4qvGf"],r = Math.floor(Math.random() * o.length + 1) - 1;
		 return  o[r];
	},
	toast:function(msg) {
	    $(".pop_ts").remove();
	    var str = "<p class=\"pop_ts\">" + msg + "</p>";
	    $("body").append(str);
	    $(".pop_ts").show();
	    //2秒后清除提示
	    setTimeout(function() {
	        $(".pop_ts").hide();
	    }, 1500);
	},
	popSmall:function(msg,titCon) {
		layer.open({
		    content: msg,
		    time: 1
		});
	},
	showLoading:function(msg){
		 $(".loading").remove();
		 msg = msg || 'Loading...';
		 var str = '<div class="loading"><span class="img"></span><p>'+msg+'</p></div>';
		 $("body").append(str);
		 $(".loading").show();
	},
	hideLoading:function(){
		 $(".loading").hide();
	},
	ajax:function(url, data, success, async, dataType, complete){
		if(data && 'function' == typeof data){
			success = data;
			data = {};
		}
		if(async && 'function' == typeof async){
			complete = async;
			async = false;
		}
		if(dataType && 'function' == typeof dataType){
			complete = dataType;
			dataType = 'json';
		}
		if(!complete || 'function' != typeof complete){
			complete = function(){};
		}
		$.ajax({
			type:'POST',
			url:url,
			async: async || false,
			dataType: dataType || 'json',
			data:data || {},
			success:success,
			error:function(XMLHttpRequest, textStatus, errorThrown){
				if(textStatus == 'timeout'){
					layer.open({
					    content: '请检查手机是否联网，或者稍后重试！',
					    time: 1
					});
				}else{
					layer.open({
					    content: textStatus,
					    time: 1
					});
				}
			},
			timeout :10000,
			complete:complete
		});
	},
	getParam:function(name){
		return window.location.search.match(new RegExp('(?:\\?|&)' + name+ '=([^&]*?)(?:&|$)', 'i')) ? RegExp.$1 : '';	
	},
	isWeiXin:function (){
	    var ua = window.navigator.userAgent.toLowerCase();
	    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
	        return true;
	    }else{
	        return false;
	    }
	},geolocation : function(callback, errorcallback) {
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r) {
			if (this.getStatus() == BMAP_STATUS_SUCCESS) {
				var myGeo = new BMap.Geocoder();// 获取用户的地址解析
				myGeo.getLocation(r.point, function(result) {
					var addComp = result.addressComponents;
					var address = addComp.district + addComp.street + addComp.streetNumber;
					callback(addComp.city,address,result);
				});
			}else{
				errorcallback(r);
			}
		}, {
			enableHighAccuracy : true
		});
	},
	addParam:function(url, name, value){
		if(url.indexOf('?')<0){
			url+='?';
		}
		return url+'&'+name+'='+encodeURIComponent(value);
	}
};
Date.prototype.format = function(fmt){
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日   
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;
};